package com.steptime.thread.threadlocal;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 每个线程需要一个独享的对象(通常是工具类，典型需要使用的类有 SimpleDate Format和 Random)
 */
public class ThreadLocalDemo {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        for (int i = 0; i < 1000; i++) {
            executorService.execute(new ParseDate(i));
        }
        executorService.shutdown();

    }

    static class ParseDate implements Runnable {
        private static final SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
        int i = 0;

        public ParseDate(int i) {
            this.i = i;
        }

        public ParseDate() {
        }

        @Override
        public void run() {
            try {
                System.out.println(i+":"+ format.parse("2020-10-07 11:40:" + i % 60));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
}
